<!-- src/views/business/order/index.vue -->
<template>
  <div class="order-management">
    <div class="page-header">
      <h1>订单管理</h1>
      <p>管理系统订单信息</p>
    </div>

    <el-card>
      <template #header>
        <span>订单列表</span>
      </template>
      
      <el-table :data="orderList" v-loading="loading">
        <el-table-column type="index" label="订单ID" width="60" />
        <el-table-column prop="orderNo" label="订单号" />
        <el-table-column prop="productName" label="产品名称" />
        <el-table-column prop="customer" label="客户" />
        <el-table-column prop="amount" label="金额">
          <template #default="{ row }">
            ¥{{ row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="下单时间" />
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button size="small" @click="handleView(row)">查看</el-button>
            <el-button size="small" type="warning" @click="handleProcess(row)">
              处理
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

const loading = ref(false)
const orderList = ref([])

const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

const getStatusType = (status) => {
  const types = {
    pending: 'warning',
    paid: 'success',
    shipped: 'primary',
    completed: 'info',
    cancelled: 'danger'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    pending: '待支付',
    paid: '已支付',
    shipped: '已发货',
    completed: '已完成',
    cancelled: '已取消'
  }
  return texts[status] || '未知'
}

const fetchOrders = async () => {
  loading.value = true
  try {
    // 模拟数据
    orderList.value = [
      {
        id: 1,
        orderNo: 'ORDER20240001',
        productName: 'iPhone 15',
        customer: '张三',
        amount: 5999,
        status: 'paid',
        createTime: '2024-01-01 10:00:00'
      },
      {
        id: 2,
        orderNo: 'ORDER20240002',
        productName: 'MacBook Pro',
        customer: '李四',
        amount: 12999,
        status: 'shipped',
        createTime: '2024-01-02 14:30:00'
      }
    ]
    pagination.total = 2
  } catch (error) {
    console.error('获取订单列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleView = (row) => {
  ElMessage.info(`查看订单: ${row.orderNo}`)
}

const handleProcess = (row) => {
  ElMessage.info(`处理订单: ${row.orderNo}`)
}

onMounted(() => {
  fetchOrders()
})
</script>

<style scoped>
.order-management {
  padding: 0;
}

.pagination-container {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>